package cn.gdut.chengcai.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gdut.chengcai.base.AccountUtil;
import cn.gdut.chengcai.base.CommentUtil;
import cn.gdut.chengcai.base.ConstantUtil;
import cn.gdut.chengcai.base.JsonUtil;
import cn.gdut.chengcai.base.MessageUtil;
import cn.gdut.chengcai.base.Pager;
import cn.gdut.chengcai.base.PagerUtil;
import cn.gdut.chengcai.base.ResponseData;
import cn.gdut.chengcai.entity.CommentInfo;
import cn.gdut.chengcai.entity.CommentInfoExample;
import cn.gdut.chengcai.entity.CourseInfo;
import cn.gdut.chengcai.entity.MemberInfo;
import cn.gdut.chengcai.service.CommentInfoService;
import cn.gdut.chengcai.service.CommentSupportInfoService;
import cn.gdut.chengcai.service.CourseInfoService;
import cn.gdut.chengcai.service.MemberInfoService;
import cn.gdut.chengcai.vo.CommentInfoVo;

@Controller
@RequestMapping("/member/comment")
public class CommentController {

	@Autowired
	protected CommentSupportInfoService commentSupportInfoService;

	@Autowired
	protected CommentInfoService commentInfoService;

	@Autowired
	protected CourseInfoService courseInfoService;

	@Autowired
	protected MemberInfoService memberInfoService;

	/**
	 * 课程评论提交操作
	 */
	@RequestMapping(value = "/course/saveCommentAction", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData<Object> saveCommentAction(@RequestBody String jsonString, HttpServletRequest request,
			HttpSession session) {
		ResponseData<Object> result = new ResponseData<>();
		result.setIsSuccess(false);
		try {
			CommentInfo param = (CommentInfo) JsonUtil.fromJson(jsonString, CommentInfo.class);
			MemberInfo memberInfo = (MemberInfo) session.getAttribute(ConstantUtil.STR_MEMBER);
			// 过滤敏感词
			/*
			 * SensitiveWordFilter filter = new SensitiveWordFilter();
			 * param.setContent(filter.replaceSensitiveWord(param.getContent(),
			 * 1, "*"));
			 */

			// 更新评论的课程对象的评论数
			CourseInfo courseInfo = courseInfoService.selectByPrimaryKey(param.getTargetId());
			if (null != courseInfo.getCommentCount()) {
				courseInfo.setCommentCount(courseInfo.getCommentCount() + 1);
			} else {
				courseInfo.setCommentCount(1L);
			}

			courseInfoService.updateByPrimaryKey(courseInfo);

			// 插入数据库之前初始化评论的一些属性
			param.setMemberId(memberInfo.getId());
			param.setMemberUsername(memberInfo.getUsername());
			param.setMemberAvatarUrl(memberInfo.getUserAvatarUrl());
			param.setIsDeleted(false);
			param.setCreateTime(new Date());

			// 插入数据库
			commentInfoService.insert(param);
			result.setMessage("您已发表评论！");
			result.setIsSuccess(true);

		} catch (Exception e) {
			result.setMessage(MessageUtil.SYSTEM_ERROR);
			// log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 我的评论
	 */

	@RequestMapping("/mine/list")
	public String commentList(HttpSession session, ModelMap map) {

		CommentInfoExample example = new CommentInfoExample();
		example.createCriteria().andIsDeletedEqualTo(false).andMemberIdEqualTo(AccountUtil.getUid(session));
		List<CommentInfo> listTotal = commentInfoService.selectByExample(example);
		Long itemCount = Long.valueOf(String.valueOf(listTotal.size()));
		Pager pager = PagerUtil.getPager(session, itemCount);
		int fromIndex = (pager.getPageNo() - 1) * pager.getPageSize();
		if (fromIndex >= listTotal.size()) {
			fromIndex = 0;
			pager.setPageNo(1);
			session.setAttribute(ConstantUtil.STR_PAGE_NO, "1");
		}
		int toIndex = fromIndex + pager.getPageSize();
		if (toIndex >= listTotal.size()) {
			toIndex = listTotal.size();
		}

		List<CommentInfo> list = listTotal.subList(fromIndex, toIndex);
		List<CommentInfoVo> voList = new ArrayList<>();
		if (list != null && list.size() > 0) {
			for (CommentInfo entity : list) {
				CommentInfoVo vo = new CommentInfoVo();
				try {
					CommentUtil.fillVo(entity, vo);
				} catch (Exception e) {
					e.printStackTrace();
				}
				voList.add(vo);
			}
		}

		map.put("list", voList);
		session.setAttribute("pager", pager);

		return "/personal/account/comment";
	}

}